﻿using System;
using DataFiller.DataStorage.Extension.Interpreters;
using DataFiller.DataStorage.Interpreters;
using DataFiller.DataStorage.Store;

namespace DataFiller.DataStorage.Extension
{
    /// <summary>
    /// holding all information of the hole export process
    /// </summary>
    public class ExportContext : IDisposable
    {
        //using this class only for excel logic, so we should use ExcelDataMap for logic require, not the IDataMap.
        public ExcelDataMap<int> DataMap { get; set; }
        public int ContinuousRowCount { get; set; }
        public InterpreterContainer Container { get; set; }

        public ExportContext(RetrieveModelCallBack modelRetriever)
        {
            ContinuousRowCount = 0;

            DataMap = new ExcelDataMap<int>();
            Container = new InterpreterContainer(modelRetriever);
            RegisterInterpreter();
            DataMap.RegistInterpreterContainer(Container);
        }

        //todo we should do this with config injection to improve maintainability
        private void RegisterInterpreter()
        {
            IInterpreter colInterpreter = new RowPlaceHolder_Interpreter();
            IInterpreter rowInterpreter = new FillPlaceHolder_Interpreter();
            IInterpreter fillInterpreter = new ColPlaceHolder_Interpreter();

            //prepare InterpreterContainer
            Container.AddInterpreter(colInterpreter);
            Container.AddInterpreter(rowInterpreter);
            Container.AddInterpreter(fillInterpreter);
        }

        #region IDisposable
        public bool disposed { get; private set; }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    DataMap.ClearRegion();
                    Container = null;
                }

                // Call the appropriate methods to clean up unmanaged resources here. 
                // Note disposing has been done.
                disposed = true;
            }
        }
        ~ExportContext()
        {
            Dispose(false);
        }
        #endregion
    }
}
